package com.cskaoyan.market.controller.admin;

import com.cskaoyan.market.annotation.RequiresPermissionsDesc;
import com.cskaoyan.market.db.domain.MarketCategory;
import com.cskaoyan.market.service.admin.MarketCategoryService;
import com.cskaoyan.market.util.ResponseUtil;
import com.cskaoyan.market.vo.admin.MarketCategoryLevelVo;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author：Katherine
 * @Package：com.cskaoyan.market.controller
 * @Project：java-project1
 * @name：CategoryController
 * @Date：2023/5/9 22:34
 * @Filename：CategoryController
 */
@RequiresAuthentication
@RestController
@RequestMapping("/admin/category")
@Validated
public class AdminCategoryController {
    private final Log logger = LogFactory.getLog(AdminCategoryController.class);
    @Autowired
    MarketCategoryService categoryService;


    // 展示全部的商品类目
    @RequiresPermissions("admin:category:list")
    @RequiresPermissionsDesc(menu = {"商场管理", "类目管理"}, button = "查询")
    @GetMapping("/list")
    public Object list() {
        System.out.println("come in CategoryController");
        // 先查询商品全部的一级类目
        List<MarketCategory> oneCategoryAllList = categoryService.selectAllOneCategory();
        // 再查询出全部的二级类目
        List<MarketCategory> twoCategoryAllList = categoryService.selectAllTwoCategory();

        // 总的返回的list，一级类目包含了所对应的二级类目
        List<MarketCategoryLevelVo> categoryAllList = new ArrayList<>();
        // 进行一级类目和二级类目的匹配
        for (MarketCategory one : oneCategoryAllList) {
            // 实例化新bean用于接收一次一级类目和二级类目的对应(每次循环接收一次)
            MarketCategoryLevelVo marketCategoryLevel = new MarketCategoryLevelVo();
            marketCategoryLevel.setId(one.getId());
            marketCategoryLevel.setName(one.getName());
            marketCategoryLevel.setKeywords(one.getKeywords());
            marketCategoryLevel.setDesc(one.getDesc());
            marketCategoryLevel.setIconUrl(one.getIconUrl());
            marketCategoryLevel.setPicUrl(one.getPicUrl());
            marketCategoryLevel.setLevel(one.getLevel());
            for (MarketCategory two : twoCategoryAllList) {
                if (one.getId().equals(two.getPid())) {
                    marketCategoryLevel.getChildren().add(two);
                }
            }
            categoryAllList.add(marketCategoryLevel);
        }
        // 转化成json格式响应给客户端
        return ResponseUtil.okList(categoryAllList);
    }

    // 查询出全部的一级类目，下拉列表
    @RequiresPermissions("admin:category:list")
    @GetMapping("/l1")
    public Object l1() {
        List<MarketCategory> marketOneCategories = categoryService.selectAllOneCategory();
        // 新建一个list和map用于返回
        List<Object> oneCategory = new ArrayList<>();
        for (MarketCategory one : marketOneCategories) {
            HashMap<String, Object> oneCategoryMap = new HashMap<>();
            oneCategoryMap.put("value", one.getId());
            oneCategoryMap.put("label", one.getName());
            oneCategory.add(oneCategoryMap);
        }
        // 响应给客户端
        return ResponseUtil.okList(oneCategory);
    }


    // 添加类目
    @RequiresPermissions("admin:category:create")
    @RequiresPermissionsDesc(menu = {"商场管理", "类目管理"}, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody MarketCategory marketCategory)  {
        // 获取当前新建的时间
        LocalDateTime parseTime = LocalDateTime.now();
        marketCategory.setAddTime(parseTime);
        marketCategory.setUpdateTime(parseTime);
        // 进行类目的新增
        categoryService.insertMarketCategory(marketCategory);
        // 进行客户端的响应
        return ResponseUtil.ok(marketCategory);
    }

    // 修改类目
    @RequiresPermissions("admin:category:update")
    @RequiresPermissionsDesc(menu = {"商场管理", "类目管理"}, button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody MarketCategory marketCategory) {

        // 通过id进行修改
        int flag = categoryService.updateMarketCategoryById(marketCategory);
        // 修改成功后响应客户端
        if (flag > 0) {
            return ResponseUtil.ok();
        } else {
            return ResponseUtil.updatedDataFailed();
        }
    }

    // 删除类目
    @RequiresPermissions("admin:category:delete")
    @RequiresPermissionsDesc(menu = {"商场管理", "类目管理"}, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody MarketCategory marketCategory)  {
        // 转化成一个对象
        marketCategory.setDeleted(true);
        // 删除是逻辑删除，把delete变成1，则通过修改即可
        categoryService.deleteMarketCategoryById(marketCategory);
        // 将删除成功的信息返回给客户端
        return ResponseUtil.ok();
    }
}

